package com.example.testproject;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import android.os.Bundle;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class Screen6 extends Activity implements View.OnClickListener, OnItemSelectedListener {
	public static String name, email, phone, destination, source, time, number;
	public String response;
	EditText number1;

	DateFormat fmtDateAndTime = DateFormat.getDateTimeInstance();
	TextView lblDateAndTime;
	Calendar myCalendar = Calendar.getInstance();
	int flag1=0,flag2=0;
	
	DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() {
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
		myCalendar.set(Calendar.YEAR, year);
		myCalendar.set(Calendar.MONTH, monthOfYear);
		myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
		flag2=1;
		updateLabel();
		}
		};
		
		TimePickerDialog.OnTimeSetListener t = new TimePickerDialog.OnTimeSetListener() {
			public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
				myCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
				myCalendar.set(Calendar.MINUTE, minute);
				flag1=1;
				updateLabel();
			}
			};
			
			private void updateLabel() {
				if(flag1==1&&flag2==1)
				lblDateAndTime.setText(fmtDateAndTime.format(myCalendar.getTime()));
				time = lblDateAndTime.getText().toString();
				//btnDate.setText(fmtDateAndTime.format(myCalendar.getTime()));
				//btnTime.setText(fmtDateAndTime.format(myCalendar.getTime()));
			}

			
			

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen6);
        Intent prev = getIntent();
        name = prev.getExtras().getString("name");
        email = prev.getExtras().getString("email");
        phone = prev.getExtras().getString("phone");
        number1 = (EditText)findViewById(R.id.numbersTextView);
        Button btnDate = (Button) findViewById(R.id.btnDate);
    	Button btnTime = (Button) findViewById(R.id.btnTime);
    	Spinner destinationSpinner = (Spinner) findViewById(R.id.to_spinner);
    	Spinner sourceSpinner = (Spinner) findViewById(R.id.from_spinner);
    	
    	
		
       
		lblDateAndTime = (TextView) findViewById(R.id.lblDateAndTime);
		destinationSpinner.setOnItemSelectedListener(this);
		sourceSpinner.setOnItemSelectedListener(this);
		btnDate.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				new DatePickerDialog(Screen6.this, d, myCalendar
						.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
						myCalendar.get(Calendar.DAY_OF_MONTH)).show();
			}
		});
        
		
		btnTime.setOnClickListener(new View.OnClickListener() {
			public  void onClick(View v) {
				new TimePickerDialog(Screen6.this, t, myCalendar
						.get(Calendar.HOUR_OF_DAY), myCalendar
						.get(Calendar.MINUTE), true).show();
				
			}
		});

		updateLabel();
		Button book_now = (Button) findViewById(R.id.book_now);
		Button puneTab01 = (Button) findViewById(R.id.puneTab01);
		Button puneTab02 = (Button) findViewById(R.id.puneTab02);
		Button puneTab03 = (Button) findViewById(R.id.puneTab03);
		Button puneTab04 = (Button) findViewById(R.id.puneTab04);
		puneTab01.setOnClickListener(this);
		puneTab02.setOnClickListener(this);
		puneTab03.setOnClickListener(this);
		puneTab04.setOnClickListener(this);
		book_now.setOnClickListener(this);
		}// onCreate

    
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.book_now:
        	number = number1.getText().toString();
			   try{
			        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			        nameValuePairs.add(new BasicNameValuePair("name", name));
					nameValuePairs.add(new BasicNameValuePair("email", email));
					nameValuePairs.add(new BasicNameValuePair("phone", phone));
					nameValuePairs.add(new BasicNameValuePair("source", source));
					nameValuePairs.add(new BasicNameValuePair("destination", destination));
					nameValuePairs.add(new BasicNameValuePair("time", time));
					nameValuePairs.add(new BasicNameValuePair("number", number));

					//.d("mon", mLogon);


					HttpClient httpclient = new DefaultHttpClient();
					HttpPost httppost = new HttpPost("http://10.0.2.2/cubito/firstclean.php");
					httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
					HttpResponse response = httpclient.execute(httppost);
					HttpEntity entity = response.getEntity();
					Log.d("http", EntityUtils.toString(entity));
					Toast.makeText(getApplicationContext(), name + email + phone + source + destination + number+ time, Toast.LENGTH_LONG).show();
			    	//main_intent = new Intent(getApplicationContext(), main.class);
					
					Intent intent = new Intent(/*FirstActivity.this*/ v.getContext(), Screen7.class);
					startActivity(intent);
					   
			    	}
			   catch(Exception e)
				{
					Log.d("httperror","Error in http connection "+e.toString());
				}
				finally
				{
					
				}

        
            break;
                        	        case R.id.puneTab01:
        	Intent intent2 = new Intent(/*FirstActivity.this*/ v.getContext(), Screen1.class);
		    startActivity(intent2);

            break;
        case R.id.puneTab02:
        	Intent intent3 = new Intent(/*FirstActivity.this*/ v.getContext(), Screen8.class);
		    startActivity(intent3);

            break;
        case R.id.puneTab03:
        	Intent intent4 = new Intent(/*FirstActivity.this*/ v.getContext(), Screen10.class);
		    startActivity(intent4);

            break;
        case R.id.puneTab04:
        	Intent intent5 = new Intent(/*FirstActivity.this*/ v.getContext(), Screen9.class);
		    startActivity(intent5);

            break;


        default:
            break;
        }
}



    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		// TODO Auto-generated method stub
		if(arg0.getItemAtPosition(0).toString().equals("Destination")){
			destination = arg0.getItemAtPosition(arg2).toString();
		}
		else{
			source = arg0.getItemAtPosition(arg2).toString();
		}
	}

	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}

}
